package h0707;

import java.util.Scanner;
/**
  * @description 报数问题
  * @author 不知名帅哥
  * @date 2024/7/30 23:06
  * @version 1.0
*/
public class ReportNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int res = getResult(n);
        System.out.println(res);
    }

    //约瑟夫环问题，使用双向循环链表解决。
    private static int getResult(int n) {
        CycleLinkedList cycleLinkedList = new CycleLinkedList();
        //初始化
        for (int i = 1; i <= n; i++) {
            cycleLinkedList.addNode(i);
        }
        Node cur=cycleLinkedList.head; //从head开始
        while (cycleLinkedList.size!=1){
            for (int i = 1; i <= 3; i++) {
                if (i==3){
                    //如果是3，删除当前节点，返回下一个节点。即为下个回合的开始节点。
                    cur=cycleLinkedList.deleteNode(cur);
                }else {
                    cur=cur.next;
                }
            }
        }
        return cur.val; //返回最后的序号
    }
}

//双向循环链表的节点类
class Node {
    int val;
    Node prev;
    Node next;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.prev = null;
    }
}

class CycleLinkedList {
    int size;
    Node head;
    Node tail;

    public CycleLinkedList() {
        this.size=0;
        this.head=null;
        this.tail=null;
    }

    //添加节点
    public void addNode(int val) {
        Node newNode = new Node(val);

        if (size>0){
            tail.next=newNode;
            newNode.prev=tail;
        }else {
            head=newNode;
        }
        tail=newNode;
        tail.next=head;
        head.prev=tail; //成为循环链表
        size++;
    }

    //删除节点
    public Node deleteNode(Node node) {

        Node prev = node.prev;
        Node next = node.next;

        prev.next=next;
        next.prev=prev;
        node.prev=node.next=null;

        if (head==node){
            this.head=next;
        }

        if (tail==node){
            this.tail=prev;
        }
        size--;
        return next;  //返回删除节点的后继方便求解。
    }
}